var network = require("../../utils/request.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head:'',
    nickName:'',
    phone:'',
    email:'',
    sex:'',
    realName:'',
    unit:'',
    consumerId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  uploadImg: function () {
    var that = this;
    wx.chooseImage({
      success(res) {
        var tempFilePaths = res.tempFilePaths
        var url = app.globalData.uploadUrl;
        wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'Filedata',
          method: 'post',
          success(res) {
            var tempRes = JSON.parse(res.data);
            if (tempRes.result == "success") {
              that.setData({
                head: tempRes.url
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = app.globalData.userInfo;
    this.setData({
      consumerId:userInfo.id,
      head: userInfo.head,
      nickName: userInfo.nickName,
      phone: userInfo.phone,
      email: userInfo.email,
      sex: userInfo.sex,
      realName: userInfo.realName,
      unit: userInfo.unit

    })
  },
  //选择性别
  selSex:function(e){
    var sex = e.currentTarget.dataset.sex;
    this.setData({
      sex:sex
    })
  },
  //提交
  formSubmit:function(e){
    var data = e.detail.value;
    data.sex = this.data.sex;
    data.head = this.data.head;
    data.consumerId = this.data.consumerId;
    if(data.phone){
      let isPhone = this.isPoneAvailable(data.phone);
      if (!isPhone) { wx.showToast({ title: "手机号码不正确", icon: 'none' });return;}
    }
    if(data.email){
      let isEmail = this.checkEmail(data.email);
      if (!isEmail) { wx.showToast({ title: "邮箱格式不正确", icon: 'none' }); return; }
    }
    
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'editConsumerInfoAPI', data, '加载中', function (res) {
      if (res.result == "success") {
        wx.showToast({ title: "提交成功" })
      } else {
        wx.showToast({ title: res.message, icon: 'none' })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  //校验手机号码
  isPoneAvailable: function(str) {
    var  re = /^1\d{10}$/
    if (re.test(str)) {
      return true;
    } else {
      return false;
    }
  },
  //校验邮箱
  checkEmail:function(str){
    var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
    if (re.test(str)) {
      return true;
    } else {
      return false;
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})